package view;

import actor.ElementType;
import actor.Monster;
import actor.Player;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SettingView extends JFrame {

    private final int LABEL_WIDTH = 60;
    private final int FIELD_X = 80;
    private int playerSize;
    private int monsterSize;

    private final String[] MONSTER_NAME = {"snack", "venom", "zerg", "lizard", "crab", "dragon"};
    private final int[] MONSTER_LIFE = {10, 20, 50, 100, 300, 1200};
    private final int[] MONSTER_POWER = {5, 25, 5, 15, 50, 120};
    private final int[] MONSTER_SPEED = {3, 5, 8, 5, 4, 3};
    private final ElementType[] MONSTER_TYPE = {ElementType.ICE, ElementType.ICE, ElementType.WATER,
            ElementType.WATER, ElementType.FIRE, ElementType.FIRE};

    JTextField nameField = new JTextField();
    JTextField lifeField = new JTextField();
    JTextField speedField = new JTextField();
    JTextField powerField = new JTextField();

    JPanel playerPanel = new JPanel();
    JPanel monsterPanel = new JPanel();

    JPanel weaponPanel = new JPanel();
    JComboBox<String> weaponBox = new JComboBox<>();
    JPanel imagePanel = new JPanel();
    JComboBox<String> imageBox = new JComboBox<>();
    JPanel selectMonsterPanel = new JPanel();
    JComboBox<String> monsterBox = new JComboBox<>();

    JPanel confirmPanel = new JPanel();
    JButton confirm = new JButton("AddPlayer");

    JPanel finishPanel = new JPanel();
    JButton finishButton = new JButton("Finish");

    JPanel addMonsterPanel = new JPanel();
    JButton addMonsterButton = new JButton("AddMonster");

    Monster[] monsters = new Monster[4];
    Player[] players = new Player[4];
    SelectView[] selectPlayer = new SelectView[4];
    SelectView[] selectMonster = new SelectView[4];

    private static final SettingView instance = new SettingView();

    public static SettingView getInstance() {
        return instance;
    }

    private SettingView() {

        init();
        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                int life = Integer.parseInt(lifeField.getText());
                int speed = Integer.parseInt(speedField.getText());
                int power = Integer.parseInt(powerField.getText());
                String weaponType = (String) weaponBox.getSelectedItem();
                String image = (String) imageBox.getSelectedItem();
                System.out.println(name + life + "-" + speed + "-" + power + weaponType + image);
                Player newPlayer = new Player(name, speed, power, life, weaponType, image);
                players[playerSize] = newPlayer;
                selectPlayer[playerSize] = new SelectView(newPlayer, true);
                playerPanel.add(selectPlayer[playerSize]);
                SwingUtilities.updateComponentTreeUI(playerPanel);
                playerSize++;
            }
        });

        addMonsterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String image = (String) monsterBox.getSelectedItem();
                int index = monsterBox.getSelectedIndex();
                Monster monster = new Monster(MONSTER_NAME[index], MONSTER_SPEED[index], MONSTER_POWER[index], MONSTER_LIFE[index], MONSTER_TYPE[index], image);
                monsters[monsterSize] = monster;
                selectMonster[monsterSize] = new SelectView(monster, false);
                monsterPanel.add(selectMonster[monsterSize]);
                SwingUtilities.updateComponentTreeUI(monsterPanel);
                monsterSize++;
            }
        });

        finishButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                GameGround gameGround = GameGround.getInstance().init(monsters, players);
                gameGround.setVisible(true);
            }
        });
        setSize(620, 523);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void init() {
        setTitle("Choose");
        setLayout(null);
        weaponBox.addItem("ICE");
        weaponBox.addItem("FIRE");
        weaponBox.addItem("WATER");
        imageBox.addItem("captain");
        imageBox.addItem("antMan");
        imageBox.addItem("hawk");
        imageBox.addItem("ironMan");
        imageBox.addItem("thor");

        monsterBox.addItem("snack");
        monsterBox.addItem("venom");
        monsterBox.addItem("zerg");
        monsterBox.addItem("lizard");
        monsterBox.addItem("crab");
        monsterBox.addItem("dragon");

        JLabel nameLabel = new JLabel("Name");
        nameLabel.setBounds(10, 0, LABEL_WIDTH, 20);
        nameField.setBounds(FIELD_X, 0, 80, 20);
        JLabel lifeLabel = new JLabel("Life");
        lifeLabel.setBounds(10, 25, LABEL_WIDTH, 20);
        lifeField.setBounds(FIELD_X, 25, 80, 20);
        JLabel speedLabel = new JLabel("Speed");
        speedLabel.setBounds(10, 50, LABEL_WIDTH, 20);
        speedField.setBounds(FIELD_X, 50, 80, 20);
        JLabel powerLabel = new JLabel("Power");
        powerLabel.setBounds(10, 75, LABEL_WIDTH, 20);
        powerField.setBounds(FIELD_X, 75, 80, 20);
        JLabel weaponLabel = new JLabel("Weapon");
        weaponLabel.setBounds(10, 100, LABEL_WIDTH, 30);
        weaponPanel.add(weaponBox);
        weaponPanel.setBounds(FIELD_X - 20, 100, 100, 30);
        JLabel imageLabel = new JLabel("Image");
        imageLabel.setBounds(10, 135, LABEL_WIDTH, 30);
        imagePanel.add(imageBox);
        imagePanel.setBounds(FIELD_X - 20, 135, 100, 30);

        confirmPanel.setBounds(10, 175, 100, 40);
        confirmPanel.add(confirm);

        finishPanel.setBounds(10, 215, 100, 40);
        finishPanel.add(finishButton);

        addMonsterPanel.setBounds(350, 5, 120, 40);
        addMonsterPanel.add(addMonsterButton);

        playerPanel.setBounds(10, 250, 590, 230);
        playerPanel.setLayout(new BoxLayout(playerPanel, BoxLayout.X_AXIS));
        playerPanel.setBorder(new LineBorder(Color.BLACK, 2));

        monsterPanel.setBounds(170, 40, 430, 207);
        monsterPanel.setLayout(new BoxLayout(monsterPanel, BoxLayout.X_AXIS));
        monsterPanel.setBorder(new LineBorder(Color.BLACK, 2));

        selectMonsterPanel.add(monsterBox);
        selectMonsterPanel.setBounds(200, 5, 120, 30);

        add(nameLabel);
        add(lifeLabel);
        add(speedLabel);
        add(powerLabel);
        add(weaponLabel);
        add(imageLabel);
        add(nameField);
        add(lifeField);
        add(speedField);

        add(powerField);
        add(weaponPanel);
        add(imagePanel);
        add(confirmPanel);
        add(playerPanel);
        add(monsterPanel);
        add(selectMonsterPanel);
        add(finishPanel);
        add(addMonsterPanel);
    }

}
